using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JGSY.CMS.LowCode.Platform.Domain.Entities;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.CmsDbContext;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Repositories
{
    /// <summary>
    /// 用户设备仓储实现
    /// 提供用户设备信息管理的数据访问功能实现，使用内存存储
    /// </summary>
    public class UserDeviceRepository : IUserDeviceRepository
    {
        private readonly LowCodeDbContext _context;
        /// <summary>
        /// 用户设备记录集合
        /// </summary>
        private readonly List<UserDevice> _list = new();

        public UserDeviceRepository(LowCodeDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有用户设备记录
        /// </summary>
        /// <returns>用户设备记录集合</returns>
        public async Task<IEnumerable<UserDevice>> GetAllAsync()
        {
            await Task.CompletedTask;
            return _list;
        }

        /// <summary>
        /// 根据标识符获取用户设备记录
        /// </summary>
        /// <param name="id">用户设备标识符</param>
        /// <returns>用户设备记录，如果不存在则返回null</returns>
        public async Task<UserDevice?> GetByIdAsync(long id)
        {
            await Task.CompletedTask;
            return _list.FirstOrDefault(e => e.Id == id);
        }

        /// <summary>
        /// 添加新的用户设备记录
        /// </summary>
        /// <param name="entity">要添加的用户设备实体</param>
        /// <returns>添加操作的异步任务</returns>
        public async Task AddAsync(UserDevice entity)
        {
            await Task.CompletedTask;
            _list.Add(entity);
        }

        /// <summary>
        /// 更新用户设备记录
        /// </summary>
        /// <param name="id">用户设备标识符</param>
        /// <param name="entity">更新后的用户设备实体</param>
        /// <returns>更新操作的异步任务</returns>
        public async Task UpdateAsync(long id, UserDevice entity)
        {
            await Task.CompletedTask;
            var idx = _list.FindIndex(e => e.Id == id);
            if (idx >= 0) _list[idx] = entity;
        }

        /// <summary>
        /// 删除用户设备记录
        /// </summary>
        /// <param name="id">用户设备标识符</param>
        /// <returns>删除操作的异步任务</returns>
        public async Task DeleteAsync(long id)
        {
            await Task.CompletedTask;
            _list.RemoveAll(e => e.Id == id);
        }
    }
}
